<?php

namespace app\company\controller\statistics\all;

use app\admin\service\UserService;
use app\BaseController;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\ConsultQuestionModel;
use app\model\CrisisBehaviorModel;
use app\model\CrisisTypeModel;
use app\model\FollowUserModel;
use app\model\InterfereBehaviorModel;
use app\model\InterfereUserModel;
use app\model\LoginLogModel;
use app\model\ProxyEvaluationResultModel;
use app\model\RatingScaleLogModel;
use app\model\RatingScaleModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskUserResultModel;
use app\model\UserModel;
use app\model\WarningReportRecordModel;
use think\App;
use think\facade\Db;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 用户比例统计
     * @return bool|string
     */
    public function userNumberPeople(): bool|string
    {
        try {
            $where[] = ['is_delete', '=', 0];
            if ($this->userData['type'] > 1){
                switch ($this->userData['level']) {
                    case 1:
                        $where[] = ['province_id', '=', $this->userData['province_id']];
                        break;
                    case 2:
                        $where[] = ['city_id', '=', $this->userData['city_id']];
                        break;
                    case 3:
                        $where[] = ['area_id', '=', $this->userData['area_id']];
                        break;
                }
            }
            $UserModel = new UserModel();
            $LoginLogModel = new LoginLogModel();
            $data['total'] = $UserModel->where('unit_id', 'in', $this->userData['subset_unit'])->count('id');
            $data['total'] += Db::table('cqfdkj.fdkj_user')->where($where)->count('id');
            $data['total'] += Db::table('testfdkj.fdkj_user')->where($where)->count('id');

            $data['admin'] = $UserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['role_mode', '<', 2]
            ])->count('id');
            $where2 = $where;
            $where2[] = ['role', '<', 2];
            $data['admin'] += Db::table('cqfdkj.fdkj_user')->where($where2)->count('id');
            $data['admin'] += Db::table('testfdkj.fdkj_user')->where($where2)->count('id');

            $data['boy'] = $UserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['sex', '=', 1]
            ])->count('id');
            $where3 = $where;
            $where3[] = ['sex', '=', 1];
            $data['boy'] += Db::table('cqfdkj.fdkj_user')->where($where3)->count('id');
            $data['boy'] += Db::table('testfdkj.fdkj_user')->where($where3)->count('id');

            $data['girl'] = $UserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['sex', '=', 2]
            ])->count('id');
            $where4 = $where;
            $where4[] = ['sex', '=', 2];
            $data['girl'] += Db::table('cqfdkj.fdkj_user')->where($where4)->count('id');
            $data['girl'] += Db::table('testfdkj.fdkj_user')->where($where4)->count('id');

            $data['online'] = $LoginLogModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['time', '>', strtotime(date("Y-m-d"))]
            ])->distinct(true)->count('uid');
            $where5 = $where;
            $where5[] = ['login_time', '>', strtotime(date("Y-m-d"))];
            $data['online'] += Db::table('cqfdkj.fdkj_user')->where($where5)->count('id');
            $data['online'] += Db::table('testfdkj.fdkj_user')->where($where5)->count('id');

            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 用户在线活跃度
     * @return bool|string
     */
    public function userOnline(): bool|string
    {
        try {
            $where[] = ['is_delete', '=', 0];
            if ($this->userData['type'] > 1){
                switch ($this->userData['level']) {
                    case 1:
                        $where[] = ['province_id', '=', $this->userData['province_id']];
                        break;
                    case 2:
                        $where[] = ['city_id', '=', $this->userData['city_id']];
                        break;
                    case 3:
                        $where[] = ['area_id', '=', $this->userData['area_id']];
                        break;
                }
            }
            $date = custom_time();
            $data = [];
            $LoginLogModel = new LoginLogModel();
            $UserModel = new UserModel();
            foreach ($date as $item) {
                $data['date'][] = date('d',strtotime($item['value']));
                $start_time = strtotime($item['start_time']);
                $end_time = strtotime($item['end_time']);
                $uid = $LoginLogModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['time', 'between', [$start_time, $end_time]]
                ])->distinct(true)->column('uid');
                $total = count($uid);
                $boy = $UserModel->where([
                    ['id','in',$uid],
                    ['sex','=',1]
                ])->count();
                $where2 = $where;
                $where2[] = ['login_time', 'between', [$start_time, $end_time]];
                $total += Db::table('cqfdkj.fdkj_user')->where($where2)->count('id');
                $total += Db::table('testfdkj.fdkj_user')->where($where2)->count('id');

                $where3 = $where;
                $where3[] = ['login_time', 'between', [$start_time, $end_time]];
                $where3[] = ['sex','=',1];
                $boy += Db::table('cqfdkj.fdkj_user')->where($where3)->count('id');
                $boy += Db::table('testfdkj.fdkj_user')->where($where3)->count('id');

                $data['boy'][] = $boy;
                $data['girl'][] = $total - $boy;
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 用户年龄段细分
     * @return bool|string
     */
    public function userAge(): bool|string
    {
        try {
            $where[] = ['is_delete', '=', 0];
            if ($this->userData['type'] > 1){
                switch ($this->userData['level']) {
                    case 1:
                        $where[] = ['province_id', '=', $this->userData['province_id']];
                        break;
                    case 2:
                        $where[] = ['city_id', '=', $this->userData['city_id']];
                        break;
                    case 3:
                        $where[] = ['area_id', '=', $this->userData['area_id']];
                        break;
                }
            }
            $UserModel = new UserModel();

            // 核心优化：用SQL直接统计各年龄段、性别的数量，避免PHP循环
            $stats = $UserModel->field([
                // 用CASE将年龄分组为1-6（对应原来的6个年龄段）
                'CASE 
                WHEN age BETWEEN 0 AND 5 THEN 1
                WHEN age BETWEEN 6 AND 10 THEN 2
                WHEN age BETWEEN 11 AND 15 THEN 3
                WHEN age BETWEEN 16 AND 20 THEN 4
                WHEN age BETWEEN 21 AND 25 THEN 5
                WHEN age > 25 THEN 6
             END AS age_group',
                'sex', // 性别（1=男，其他=女）
                'COUNT(id) AS count' // 统计该分组数量
            ])
                ->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['role_mode', '=', 3]
                ])
                ->group('age_group, sex') // 按年龄段和性别分组
                ->select()
                ->toArray();

            $stats2 = Db::table('cqfdkj.fdkj_user')->field([
                // 用CASE将年龄分组为1-6（对应原来的6个年龄段）
                'CASE 
                WHEN age BETWEEN 0 AND 5 THEN 1
                WHEN age BETWEEN 6 AND 10 THEN 2
                WHEN age BETWEEN 11 AND 15 THEN 3
                WHEN age BETWEEN 16 AND 20 THEN 4
                WHEN age BETWEEN 21 AND 25 THEN 5
                WHEN age > 25 THEN 6
             END AS age_group',
                'sex', // 性别（1=男，其他=女）
                'COUNT(id) AS count' // 统计该分组数量
            ])
                ->where($where)
                ->group('age_group, sex') // 按年龄段和性别分组
                ->select()
                ->toArray();

            $stats3 = Db::table('testfdkj.fdkj_user')->field([
                // 用CASE将年龄分组为1-6（对应原来的6个年龄段）
                'CASE 
                WHEN age BETWEEN 0 AND 5 THEN 1
                WHEN age BETWEEN 6 AND 10 THEN 2
                WHEN age BETWEEN 11 AND 15 THEN 3
                WHEN age BETWEEN 16 AND 20 THEN 4
                WHEN age BETWEEN 21 AND 25 THEN 5
                WHEN age > 25 THEN 6
             END AS age_group',
                'sex', // 性别（1=男，其他=女）
                'COUNT(id) AS count' // 统计该分组数量
            ])
                ->where($where)
                ->group('age_group, sex') // 按年龄段和性别分组
                ->select()
                ->toArray();

            $stats = array_merge($stats,$stats2,$stats3);
            // 初始化统计结果（与原结构一致）
            $data2 = [
                'boy' => 0, 'girl' => 0,
                'boy2' => 0, 'girl2' => 0,
                'boy3' => 0, 'girl3' => 0,
                'boy4' => 0, 'girl4' => 0,
                'boy5' => 0, 'girl5' => 0,
                'boy6' => 0, 'girl6' => 0,
            ];

            // 映射数据库统计结果到$data2（仅循环统计结果，最多12条）
            foreach ($stats as $item) {
                $ageGroup = $item['age_group'];
                $sex = $item['sex'];
                $count = $item['count'];

                // 生成键名（如age_group=1且sex=1对应'boy'）
                $key = ($sex == 1 ? 'boy' : 'girl') . ($ageGroup > 1 ? $ageGroup : '');
                if (isset($data2[$key])) {
                    $data2[$key] += $count;
                }
            }

            return success($data2);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 预警结果统计
     * @return bool|string
     */
    public function warningPeopleNum(): bool|string
    {
        try {
            $WarningReportRecordModel = new WarningReportRecordModel();
            //今日
            $data['day'] = $WarningReportRecordModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['time','>',strtotime(date("Y-m-d"))]
            ])->distinct(true)->count('uid');
            $data['day'] += Db::table('testfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['is_warning','=',1],
                ['create_time','>',strtotime(date("Y-m-d"))]
            ])->distinct(true)->count('user_id');
            $data['day'] += Db::table('cqfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['is_warning','=',1],
                ['create_time','>',strtotime(date("Y-m-d"))]
            ])->distinct(true)->count('user_id');

            //历史
            $data['history'] = $WarningReportRecordModel->where('unit_id','in',$this->userData['subset_unit'])->distinct(true)->count('uid');
            $data['history'] += Db::table('testfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['is_warning','=',1]
            ])->distinct(true)->count('user_id');
            $data['history'] += Db::table('cqfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['is_warning','=',1]
            ])->distinct(true)->count('user_id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 累计测评结果统计
     * @return bool|string
     */
    public function resultNum(): bool|string
    {
        try {
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $data['wireless'] = 0;
            $data['task'] = $TaskUserResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            $data['task'] += Db::table('testfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['type','=',2]
            ])->count('id');
            $data['task'] += Db::table('cqfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['type','=',2]
            ])->count('id');
            $data['self'] = $SelfEvaluationResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            $data['self'] += Db::table('testfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['type','=',1]
            ])->count('id');
            $data['self'] += Db::table('cqfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['type','=',1]
            ])->count('id');
            $data['proxy'] = $ProxyEvaluationResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 当月测评人数走势
     * @return bool|string
     */
    public function monthTrend(): bool|string
    {
        try {
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $UserModel = new UserModel();
            $date = custom_time([date("Y-m-01"),date('Y-m-t')]);
            $data = [];
            foreach ($date as $item){
                $data['date'][] = $item['value'];
                $start_time = strtotime($item['start_time']);
                $end_time = strtotime($item['end_time']);
                $task = $TaskUserResultModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct(true)->column('uid');
                $self = $SelfEvaluationResultModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct(true)->column('uid');
                $proxy = $ProxyEvaluationResultModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct(true)->column('uid');
                $uids = array_merge($task,$self,$proxy);
                $uids = array_unique($uids);
                if (empty($uids)){
                    $total = 0;
                    $boy = 0;
                }else{
                    $total = count($uids);
                    $boy = $UserModel->where([
                        ['id','in',$uids],
                        ['sex','=',1]
                    ])->count('id');
                }

                $user_id = Db::table('testfdkj.fdkj_task_test_report')->where([
                    ['is_delete','=',0],
                    ['create_time','between',[$start_time,$end_time]]
                ])->distinct(true)->column('user_id');
                $total += count($user_id);
                $boy += Db::table('testfdkj.fdkj_user')->where([
                    ['id','in',$user_id],
                    ['sex','=',1]
                ])->count('id');

                $user_id = Db::table('cqfdkj.fdkj_task_test_report')->where([
                    ['is_delete','=',0],
                    ['create_time','between',[$start_time,$end_time]]
                ])->distinct(true)->column('user_id');
                $total += count($user_id);
                $boy += Db::table('cqfdkj.fdkj_user')->where([
                    ['id','in',$user_id],
                    ['sex','=',1]
                ])->count('id');

                $girl = $total - $boy;
                $data['boy'][] = $boy;
                $data['girl'][] = $girl;
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 危机人数统计
     * @return bool|string
     */
    public function interfereNumberPeople(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $monthStart = strtotime(date("Y-m-01 00:00:00")); // 精确到当月第一天0点

            // 当月危机人数：数据库层面去重计数，减少内存占用
            $total = $InterfereUserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']],
                ['time', '>=', $monthStart] // 改为>=更精确匹配当月数据
            ])->distinct('uid')->count('uid');

            $total += Db::table('cqfdkj.fdkj_task_test_report_crisis_intervention')->where([
                ['is_delete','=',0],
                ['create_time','>=',$monthStart]
            ])->distinct(true)->count('user_id');
            $total += Db::table('testfdkj.fdkj_task_test_report_crisis_intervention')->where([
                ['is_delete','=',0],
                ['create_time','>=',$monthStart]
            ])->distinct(true)->count('user_id');

            // 历史危机人数：同样使用数据库去重计数
            $history = $InterfereUserModel->where([
                ['unit_id', 'in', $this->userData['subset_unit']]
            ])->distinct('uid')->count('uid');
            $history += Db::table('cqfdkj.fdkj_task_test_report_crisis_intervention')
                ->where('is_delete','=',0)
                ->distinct(true)->count('user_id');
            $history += Db::table('testfdkj.fdkj_task_test_report_crisis_intervention')
                ->where('is_delete','=',0)
                ->distinct(true)->count('user_id');

            return success([
                'total' => $total,
                'history' => $history
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(), [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 危机男女比率
     * @return bool|string
     */
    public function interfereMenWomen(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $uid = $InterfereUserModel->where([
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->column('uid');
            if (!empty($uid)){
                $data['total'] = count($uid);
                $UserModel = new UserModel();
                $data['boy'] = $UserModel->where([
                    ['id','in',$uid],
                    ['sex','=',1]
                ])->count();
            }else{
                $data['total'] = 0;
                $data['boy'] = 0;
            }
            $user_id = Db::table('cqfdkj.fdkj_task_test_report_crisis_intervention')
                ->where('is_delete','=',0)
                ->distinct(true)->column('user_id');
            $data['total'] += count($user_id);
            $data['boy'] += Db::table('cqfdkj.fdkj_user')->where([
                ['id','in',$user_id],
                ['sex','=',1]
            ])->count('id');
            $user_id = Db::table('testfdkj.fdkj_task_test_report_crisis_intervention')
                ->where('is_delete','=',0)
                ->distinct(true)->column('user_id');
            $data['total'] += count($user_id);
            $data['boy'] += Db::table('testfdkj.fdkj_user')->where([
                ['id','in',$user_id],
                ['sex','=',1]
            ])->count('id');

            $data['girl'] = $data['total'] - $data['boy'];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 异常类型
     * @return bool|string
     */
    public function interfereExceptionType(): bool|string
    {
        try {
            $InterfereUserModel = new InterfereUserModel();
            $InterfereBehaviorModel = new InterfereBehaviorModel();
            $CrisisBehaviorModel = new CrisisBehaviorModel();
            $Behavior = $CrisisBehaviorModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['status','=',1]
            ])->select()->toArray();
            $data = [];
            foreach ($Behavior as $value){
                if (empty($data[$value['name']])) {
                    $data[$value['name']] = 0;
                }
                $interfere_id = $InterfereBehaviorModel->where('behavior_id','=',$value['id'])->distinct(true)->column('interfere_id');
                $data[$value['name']] += $InterfereUserModel->where('id','in',$interfere_id)->distinct(true)->count('uid');
            }

            $type = Db::table('cqfdkj.fdkj_task_test_report_crisis_intervention')->alias('a')
                ->field('a.abnormal_type_id,count(a.user_id) as num,b.name')
                ->join('cqfdkj.fdkj_crisis_intervention_abnormal_type b','a.abnormal_type_id=b.id')
                ->where('a.is_delete','=',0)->group('a.abnormal_type_id')->select()->toArray();
            $type2 = Db::table('testfdkj.fdkj_task_test_report_crisis_intervention')->alias('a')
                ->field('a.abnormal_type_id,count(a.user_id) as num,b.name')
                ->join('testfdkj.fdkj_crisis_intervention_abnormal_type b','a.abnormal_type_id=b.id')
                ->where('a.is_delete','=',0)->group('a.abnormal_type_id')->select()->toArray();
            $type = array_merge($type,$type2);
            foreach ($type as $value){
                if (empty($data[$value['name']])) {
                    $data[$value['name']] = 0;
                }
                $data[$value['name']] += $value['num'];
            }

            $data2 = [];
            foreach ($data as $key => $value){
                $data2['name'][] = $key;
                $data2['num'][] = $value;
            }
            return success($data2);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 问题类型人数统计
     * @return bool|string
     */
    public function interfereQuestionType(): bool|string
    {
        try {
            $CrisisTypeModel = new CrisisTypeModel();
            $InterfereUserModel = new InterfereUserModel();
            $type = $CrisisTypeModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['status','=',1]
            ])->select()->toArray();
            $data = [];
            $people_num = 0;
            foreach ($type as $value){
                if (empty($data[$value['name']])) {
                    $data[$value['name']] = 0;
                }
                $num = $InterfereUserModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['type_id','=',$value['id']]
                ])->distinct(true)->count('uid');
                $people_num += $num;
                $data[$value['name']] += $num;
            }


            $type = Db::table('cqfdkj.fdkj_task_test_report_crisis_intervention')->alias('a')
                ->field('a.warning_type_id,count(a.user_id) as num,b.name')
                ->join('cqfdkj.fdkj_crisis_intervention_warning_type b','a.warning_type_id=b.id')
                ->where('a.is_delete','=',0)->group('a.warning_type_id')->select()->toArray();
            $type2 = Db::table('testfdkj.fdkj_task_test_report_crisis_intervention')->alias('a')
                ->field('a.warning_type_id,count(a.user_id) as num,b.name')
                ->join('testfdkj.fdkj_crisis_intervention_warning_type b','a.warning_type_id=b.id')
                ->where('a.is_delete','=',0)->group('a.warning_type_id')->select()->toArray();
            $type = array_merge($type,$type2);
            foreach ($type as $value){
                if (empty($data[$value['name']])) {
                    $data[$value['name']] = 0;
                }
                $people_num += $value['num'];
                $data[$value['name']] += $value['num'];
            }


            $data2 = [];
            foreach ($data as $key => $value){
                $data2['name'][] = $key;
                $data2['num'][] = $value;
            }
            $total = $InterfereUserModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');

            $total += Db::table('cqfdkj.fdkj_task_test_report_crisis_intervention')->where('is_delete','=',0)->count('id');
            $total += Db::table('testfdkj.fdkj_task_test_report_crisis_intervention')->where('is_delete','=',0)->count('id');

            return success([
                'data' => $data2,
                'people_num' => $people_num,
                'total' => $total
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 危机干预及重点关注人员统计
     * @return bool|string
     */
    public function interfereStaff(): bool|string
    {
        try {
            $UserModel = new UserModel();
            $InterfereUserModel = new InterfereUserModel();
            $FollowUserModel = new FollowUserModel();
            //干预
            $uids = $InterfereUserModel->where([
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->column('uid');

            $InterfereTotal = count($uids);
            $InterfereBoy = $UserModel->where([
                ['id','in',$uids],
                ['sex','=',1]
            ])->count('id');
            // 一次性查询获取总人数和男性人数（通过关联用户表，避免中间数组）
            $stats = Db::table('cqfdkj.fdkj_task_test_report_crisis_intervention')->alias('a')
                ->leftJoin('cqfdkj.fdkj_user u', 'a.user_id = u.id') // 关联用户表
                ->where('a.is_delete', '=', 0) // 明确表别名，避免字段歧义
                ->field([
                    'COUNT(DISTINCT a.user_id) as total', // 去重统计总用户数
                    'SUM(CASE WHEN u.sex = 1 THEN 1 ELSE 0 END) as boy' // 统计男性用户数
                ])
                ->find();
            $InterfereTotal += $stats['total'] ?? 0; // 累加总人数
            $InterfereBoy += $stats['boy'] ?? 0; // 累加男性人数
            $stats = Db::table('testfdkj.fdkj_task_test_report_crisis_intervention')->alias('a')
                ->leftJoin('testfdkj.fdkj_user u', 'a.user_id = u.id') // 关联用户表
                ->where('a.is_delete', '=', 0) // 明确表别名，避免字段歧义
                ->field([
                    'COUNT(DISTINCT a.user_id) as total', // 去重统计总用户数
                    'SUM(CASE WHEN u.sex = 1 THEN 1 ELSE 0 END) as boy' // 统计男性用户数
                ])
                ->find();
            $InterfereTotal += $stats['total'] ?? 0; // 累加总人数
            $InterfereBoy += $stats['boy'] ?? 0; // 累加男性人数
            $InterfereGirl = $InterfereTotal - $InterfereBoy;

            //关注
            $uids = $FollowUserModel->where([
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->column('uid');

            $FollowTotal = count($uids);
            $FollowBoy = $UserModel->where([
                ['id','in',$uids],
                ['sex','=',1]
            ])->count('id');

            $stats = Db::table('testfdkj.fdkj_task_test_report_follow')->alias('a')
                ->leftJoin('testfdkj.fdkj_user u', 'a.user_id = u.id') // 关联用户表
                ->where('a.is_delete', '=', 0) // 明确表别名，避免字段歧义
                ->field([
                    'COUNT(DISTINCT a.user_id) as total', // 去重统计总用户数
                    'SUM(CASE WHEN u.sex = 1 THEN 1 ELSE 0 END) as boy' // 统计男性用户数
                ])
                ->find();
            $FollowTotal += $stats['total'] ?? 0; // 累加总人数
            $FollowBoy += $stats['boy'] ?? 0; // 累加男性人数
            $stats = Db::table('cqfdkj.fdkj_task_test_report_follow')->alias('a')
                ->leftJoin('cqfdkj.fdkj_user u', 'a.user_id = u.id') // 关联用户表
                ->where('a.is_delete', '=', 0) // 明确表别名，避免字段歧义
                ->field([
                    'COUNT(DISTINCT a.user_id) as total', // 去重统计总用户数
                    'SUM(CASE WHEN u.sex = 1 THEN 1 ELSE 0 END) as boy' // 统计男性用户数
                ])
                ->find();

            $FollowTotal += $stats['total'] ?? 0; // 累加总人数
            $FollowBoy += $stats['boy'] ?? 0; // 累加男性人数

            $FollowGirl = $FollowTotal - $FollowBoy;

            $date = custom_time();
            $data = [];
            foreach ($date as $value){
                $data['date'][] = date('d',strtotime($value['value']));
                $start_time = strtotime($value['start_time']);
                $end_time = strtotime($value['end_time']);

                $interfere = $InterfereUserModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct(true)->count('uid');
//                $interfere += Db::table('testfdkj.fdkj_task_test_report_crisis_intervention')->where([
//                    ['is_delete','=',0],
//                    ['create_time','between',[$start_time,$end_time]]
//                ])->distinct(true)->count('user_id');
//                $interfere += Db::table('cqfdkj.fdkj_task_test_report_crisis_intervention')->where([
//                    ['is_delete','=',0],
//                    ['create_time','between',[$start_time,$end_time]]
//                ])->distinct(true)->count('user_id');

                $data['interfere'][] = $interfere;

                $follow = $FollowUserModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct(true)->count('uid');
//                $follow += Db::table('testfdkj.fdkj_task_test_report_follow')->where([
//                    ['is_delete','=',0],
//                    ['create_time','between',[$start_time,$end_time]]
//                ])->distinct(true)->count('user_id');
//                $follow += Db::table('cqfdkj.fdkj_task_test_report_follow')->where([
//                    ['is_delete','=',0],
//                    ['create_time','between',[$start_time,$end_time]]
//                ])->distinct(true)->count('user_id');

                $data['follow'][] = $follow;

            }
            return success([
                'data' => $data,
                'interfere_boy' => $InterfereBoy,
                'interfere_girl' => $InterfereGirl,
                'follow_boy' => $FollowBoy,
                'follow_girl' => $FollowGirl
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 本月咨询完结人数
     * @return bool|string
     */
    public function consultMonthOnlineEndNum(): bool|string
    {
        try {
            $y_start_time = strtotime(date('Y-m-01'));
            $y_end_time = strtotime(date('Y-m-t 23:59:59'));
            $where = [];
            $where[] = ['end_time','between',[$y_start_time,$y_end_time]];
            $where[] = ['is_end','=',1];
            $where[] = ['unit_id','in',$this->userData['subset_unit']];
            $ConsultOnlineModel = new ConsultOnlineModel();
            //本月咨询完结人数
            $m_end_num = $ConsultOnlineModel->where($where)->distinct(true)->count('uid');
            $m_end_num += Db::table('testfdkj.fdkj_online_reservation_record')->where([
                ['end_time','between',[$y_start_time,$y_end_time]],
                ['is_end','=',1]
            ])->distinct(true)->count('uid');
            $m_end_num += Db::table('cqfdkj.fdkj_online_reservation_record')->where([
                ['end_time','between',[$y_start_time,$y_end_time]],
                ['is_end','=',1]
            ])->distinct(true)->count('uid');

            //历史咨询完结人数
            $end_num = $ConsultOnlineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',1]
            ])->distinct(true)->count('uid');
            $end_num += Db::table('testfdkj.fdkj_online_reservation_record')->where([
                ['is_end','=',1]
            ])->distinct(true)->count('uid');
            $end_num += Db::table('cqfdkj.fdkj_online_reservation_record')->where([
                ['is_end','=',1]
            ])->distinct(true)->count('uid');


            //本月占比
            $m_ratio = $end_num?round($m_end_num/$end_num*100,1):0;
            return success([
                'm_ratio' => $m_ratio,
                'm_end_num' => $m_end_num,
                'end_num' => $end_num,
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 本月预约完结人数
     * @return bool|string
     */
    public function consultMonthOfflineEndNum(): bool|string
    {
        try {
            $y_start_time = strtotime(date('Y-m-01'));
            $y_end_time = strtotime(date('Y-m-t 23:59:59'));
            $ConsultOfflineModel = new ConsultOfflineModel();
            //本月完结人数
            $m_end_num = $ConsultOfflineModel->where([
                ['end_time','between',[$y_start_time,$y_end_time]],
                ['is_end','=',1],
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->count('uid');
            $m_end_num += Db::table('testfdkj.fdkj_offline_reservation_record')->where([
                ['end_time','between',[$y_start_time,$y_end_time]],
                ['is_end','=',1]
            ])->distinct(true)->count('uid');
            $m_end_num += Db::table('cqfdkj.fdkj_offline_reservation_record')->where([
                ['end_time','between',[$y_start_time,$y_end_time]],
                ['is_end','=',1]
            ])->distinct(true)->count('uid');

            //历史完结人数
            $end_num = $ConsultOfflineModel->where([
                ['is_end','=',1],
                ['unit_id','in',$this->userData['subset_unit']]
            ])->distinct(true)->count('uid');
            $end_num += Db::table('testfdkj.fdkj_offline_reservation_record')->where([
                ['is_end','=',1]
            ])->distinct(true)->count('uid');
            $end_num += Db::table('cqfdkj.fdkj_offline_reservation_record')->where([
                ['is_end','=',1]
            ])->distinct(true)->count('uid');

            //本月占比
            $m_ratio = $end_num?round($m_end_num/$end_num*100,1):0;
            return success([
                'm_ratio' => $m_ratio,
                'm_end_num' => $m_end_num,
                'end_num' => $end_num,
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 全年人数走势图
     * @return bool|string
     */
    public function consultYearTrend(): bool|string
    {
        try {
            $data = [];
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            for ($i=1;$i<=12;$i++){
                $start_time = strtotime(date('Y-'.$i.'-01'));
                $end_time = strtotime(date('Y-'.$i.'-t 23:59:59'));
                $data['date'][] = $i.'月';
                //咨询
                $zx_num = $ConsultOnlineModel->where([
                    ['time','between',[$start_time,$end_time]],
                    ['unit_id','in',$this->userData['subset_unit']]
                ])->distinct(true)->count('uid');
                $zx_num += Db::table('cqfdkj.fdkj_online_reservation_record')->where([
                    ['create_time','between',[$start_time,$end_time]],
                ])->distinct(true)->count('uid');
                $zx_num += Db::table('testfdkj.fdkj_online_reservation_record')->where([
                    ['create_time','between',[$start_time,$end_time]],
                ])->distinct(true)->count('uid');
                $data['zx_num'][] = $zx_num;

                //预约
                $yy_num = $ConsultOfflineModel->where([
                    ['time','between',[$start_time,$end_time]],
                    ['unit_id','in',$this->userData['subset_unit']]
                ])->distinct(true)->count('uid');
                $yy_num += Db::table('cqfdkj.fdkj_offline_reservation_record')->where([
                    ['create_time','between',[$start_time,$end_time]],
                ])->distinct(true)->count('uid');
                $yy_num += Db::table('testfdkj.fdkj_offline_reservation_record')->where([
                    ['create_time','between',[$start_time,$end_time]],
                ])->distinct(true)->count('uid');

                $data['yy_num'][] = $yy_num;
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 咨询/预约次数
     * @return bool|string
     */
    public function consultNum(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            //咨询
            $data['online'] = $ConsultOnlineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',0],
            ])->count('id');
            $data['online'] += Db::table('cqfdkj.fdkj_online_reservation_record')->where([
                ['is_end','=',0],
            ])->count('id');
            $data['online'] += Db::table('testfdkj.fdkj_online_reservation_record')->where([
                ['is_end','=',0],
            ])->count('id');

            //预约
            $data['offline'] = $ConsultOfflineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',0],
            ])->count('id');
            $data['offline'] += Db::table('cqfdkj.fdkj_offline_reservation_record')->where([
                ['is_end','=',0],
            ])->count('id');
            $data['offline'] += Db::table('testfdkj.fdkj_offline_reservation_record')->where([
                ['is_end','=',0],
            ])->count('id');

            //咨询-完结
            $data['online_end'] = $ConsultOnlineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',1],
            ])->count('id');
            $data['online_end'] += Db::table('cqfdkj.fdkj_online_reservation_record')->where([
                ['is_end','=',1],
            ])->count('id');
            $data['online_end'] += Db::table('testfdkj.fdkj_online_reservation_record')->where([
                ['is_end','=',1],
            ])->count('id');

            //预约-完结
            $data['offline_end'] = $ConsultOfflineModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['is_end','=',1],
            ])->count('id');
            $data['offline_end'] += Db::table('cqfdkj.fdkj_offline_reservation_record')->where([
                ['is_end','=',1],
            ])->count('id');
            $data['offline_end'] += Db::table('testfdkj.fdkj_offline_reservation_record')->where([
                ['is_end','=',1],
            ])->count('id');

            //总数
            $data['total'] = $data['online']+$data['offline']+$data['online_end']+$data['offline_end'];
            //咨询占比
            $data['online_ratio'] = $data['online']?round($data['online']/$data['total']*100,1):0;
            //预约占比
            $data['offline_ratio'] = $data['offline']?round($data['offline']/$data['total']*100,1):0;
            //咨询-完结占比
            $data['online_end_ratio'] = $data['online_end']?round($data['online_end']/$data['total']*100,1):0;
            //预约-完结占比
            $data['offline_end_ratio'] = $data['offline_end']?round($data['offline_end']/$data['total']*100,1):0;
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 问题类型数据统计
     * @return bool|string
     */
    public function consultQuestionType(): bool|string
    {
        try {
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultOfflineModel = new ConsultOfflineModel();
            $ConsultQuestionModel = new ConsultQuestionModel();
            $question_info = $ConsultQuestionModel->where('status','=',1)->find();
            $question_id = $question_info['question_id'];
            $data['title'] = $question_info['name'];
            for ($i=1;$i<=12;$i++){
                $start_time = strtotime(date('Y-'.$i.'-01'));
                $end_time = strtotime(date('Y-'.$i.'-t 23:59:59'));
                $data['trend_date'][] = $i.'月';
                $offline = $ConsultOfflineModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['question_id','=',$question_id],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct()->count('uid');
                $online = $ConsultOnlineModel->where([
                    ['unit_id','in',$this->userData['subset_unit']],
                    ['question_id','=',$question_id],
                    ['time','between',[$start_time,$end_time]]
                ])->distinct()->count('uid');
                $data['trend_num'][] = $offline+$online;
            }
            $question = $ConsultQuestionModel->field('id,name')->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['status','=',1]
            ])->select()->toArray();
            $list = [];
            foreach ($question as $item) {
                if (empty($list[$item['name']])){
                    $list[$item['name']] = 0;
                }
                $list[$item['name']] += $ConsultOfflineModel->where('question_id','=',$item['id'])->distinct()->count('uid');
                $list[$item['name']] += $ConsultOnlineModel->where('question_id','=',$item['id'])->distinct()->count('uid');
            }

            $type = Db::table('cqfdkj.fdkj_offline_reservation_record')->alias('a')
                ->field('a.question_category_id,count(a.uid) as num,b.name')
                ->join('cqfdkj.fdkj_psychological_question_category b','a.question_category_id=b.id')
                ->where('a.is_delete','=',0)
                ->group('a.question_category_id')
                ->select()->toArray();
            $type2 = Db::table('testfdkj.fdkj_offline_reservation_record')->alias('a')
                ->field('a.question_category_id,count(a.uid) as num,b.name')
                ->join('testfdkj.fdkj_psychological_question_category b','a.question_category_id=b.id')
                ->where('a.is_delete','=',0)
                ->group('a.question_category_id')
                ->select()->toArray();
            $type3 = Db::table('testfdkj.fdkj_online_reservation_record')->alias('a')
                ->field('a.question_category_id,count(a.uid) as num,b.name')
                ->join('testfdkj.fdkj_psychological_question_category b','a.question_category_id=b.id')
                ->where('a.is_delete','=',0)
                ->group('a.question_category_id')
                ->select()->toArray();
            $type4 = Db::table('cqfdkj.fdkj_online_reservation_record')->alias('a')
                ->field('a.question_category_id,count(a.uid) as num,b.name')
                ->join('cqfdkj.fdkj_psychological_question_category b','a.question_category_id=b.id')
                ->where('a.is_delete','=',0)
                ->group('a.question_category_id')
                ->select()->toArray();
            $type = array_merge($type,$type2,$type3,$type4);
            foreach ($type as $item){
                if (empty($list[$item['name']])){
                    $list[$item['name']] = 0;
                }
                $list[$item['name']] += $item['num'];
            }

            $list2 = [];
            foreach ($list as $key => $value){
                $list2[] = [
                    'name' => $key,
                    'num' => $value,
                ];
            }
            if (!empty($list2)) {
                $field = array_column($list2,'num');
                // 假设按’field’字段升序排序
                array_multisort($field, SORT_DESC, $list2);
            }
            $data['list'] = $list2;
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 测评总数统计
     * @return bool|string
     */
    public function evaluation(): bool|string
    {
        try {
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $data = [
                'total_num' => 0,
                'time' => time()
            ];
            $data['total_num'] += $TaskUserResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            $data['total_num'] += Db::table('testfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['type','=',2]
            ])->count('id');
            $data['total_num'] += Db::table('cqfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['type','=',2]
            ])->count('id');
            $data['total_num'] += $SelfEvaluationResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            $data['total_num'] += Db::table('testfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['type','=',1]
            ])->count('id');
            $data['total_num'] += Db::table('cqfdkj.fdkj_task_test_report')->where([
                ['is_delete','=',0],
                ['type','=',1]
            ])->count('id');
            $data['total_num'] += $ProxyEvaluationResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 用户总数统计
     * @return bool|string
     */
    public function user(): bool|string
    {
        try {
            $data = [
                'total_num' => 0,
                'time' => time()
            ];
            $UserModel = new UserModel();
            $data['total_num'] += $UserModel->count();
            $data['total_num'] += Db::table('testfdkj.fdkj_user')->where('is_delete','=',0)->count();
            $data['total_num'] += Db::table('cqfdkj.fdkj_user')->where('is_delete','=',0)->count();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 累计综合数据统计
     * @return bool|string
     */
    public function comprehensive(): bool|string
    {
        try {
            $param = $this->request->param();
            $RatingScaleModel = new RatingScaleModel();
            $RatingScaleCount = $RatingScaleModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            $RatingScaleLogModel = new RatingScaleLogModel();
            $data = $RatingScaleLogModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['source','in',[1,2,3,4,5]]
            ])->with([
                'RatingScale' => function ($query) {
                    $query->with(['ratingScaleTypeName']);
                },
                'user' => function ($query) {
                    $query->with(['unitName']);
                }
            ])->order('id desc')->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item){
                $item['department_name'] = $item['unit_name'];
            }
            $log_num = $RatingScaleLogModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['source','in',[1,2,3,4,5]]
            ])->count('id');
            $people_num = $RatingScaleLogModel->where([
                ['unit_id','in',$this->userData['subset_unit']],
                ['source','in',[1,2,3,4,5]]
            ])->distinct(true)->count('uid');

            $RatingScaleCount += Db::table('testfdkj.fdkj_rating_scale')->where('is_delete','=',0)->count();
            $RatingScaleCount += Db::table('cqfdkj.fdkj_rating_scale')->where('is_delete','=',0)->count();
            $log_num += Db::table('testfdkj.fdkj_task_test_report')->where('is_delete','=',0)->count();
            $log_num += Db::table('cqfdkj.fdkj_task_test_report')->where('is_delete','=',0)->count();
            $people_num += Db::table('testfdkj.fdkj_task_test_report')->where('is_delete','=',0)->distinct(true)->count('user_id');
            $people_num += Db::table('cqfdkj.fdkj_task_test_report')->where('is_delete','=',0)->distinct(true)->count('user_id');
            return success([
                'RatingScaleCount' => $RatingScaleCount,
                'people_num' => $people_num,
                'log_num' => $log_num,
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }



    /**
     * 测评总数统计
     * @return bool|string
     */
    public function evaluation2(): bool|string
    {
        try {
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $data = [
                'total_num' => 0,
                'time' => time()
            ];
            $data['total_num'] += $TaskUserResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            $data['total_num'] += $SelfEvaluationResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');
            $data['total_num'] += $ProxyEvaluationResultModel->where('unit_id','in',$this->userData['subset_unit'])->count('id');

            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 用户总数统计
     * @return bool|string
     */
    public function user2(): bool|string
    {
        try {
            $data = [
                'total_num' => 0,
                'time' => time()
            ];
            $UserModel = new UserModel();
            $data['total_num'] += $UserModel->where('unit_id','in',$this->userData['subset_unit'])->count();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 测评总数统计
     * @return bool|string
     */
    public function evaluation3(): bool|string
    {
        try {
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $data = [
                'total_num' => 0,
                'time' => time()
            ];
            $data['total_num'] += $TaskUserResultModel->where('unit_id','=',$this->userData['unit_id'])->count('id');
            $data['total_num'] += $SelfEvaluationResultModel->where('unit_id','=',$this->userData['unit_id'])->count('id');
            $data['total_num'] += $ProxyEvaluationResultModel->where('unit_id','=',$this->userData['unit_id'])->count('id');

            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 用户总数统计
     * @return bool|string
     */
    public function user3(): bool|string
    {
        try {
            $data = [
                'total_num' => 0,
                'time' => time()
            ];
            $UserModel = new UserModel();
            $data['total_num'] += $UserModel->where('unit_id','=',$this->userData['unit_id'])->count();

            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}